<template>
    <section>
        <el-dropdown
                split-button
                type="primary"
                @command="handleCommand">
            操作
            <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="detail"
                                  v-permission="installPermissions.permissions.farmer.edit"
                >
                    详情
                </el-dropdown-item>
                <el-dropdown-item command="edit"
                                  v-permission="installPermissions.permissions.farmer.edit"
                >
                    编辑农户
                </el-dropdown-item>
                <el-dropdown-item command="upload"
                                  v-permission="installPermissions.permissions.farmer.edit">
                    上传附件
                </el-dropdown-item>
                <el-dropdown-item command="delete"
                                  v-permission="installPermissions.permissions.farmer.del"
                >
                    删除农户
                </el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>
    </section>
</template>
<style>
</style>
<script>
    export default {
        props: {
            row: {
                type: Object,
                default: function () {
                    return {};
                }
            },
            col: {
                type: Object,
                default: function () {
                    return {};
                }
            }
        },
        data() {
            return {
                headers: {
                    Authorization: ""
                },
                importUrl: this.$config.http.prefix + "/common/upload"
            }

        },
        methods: {
            handleCommand(cmd) {
                switch (cmd) {
                    case "edit":
                        return this.handleEdit();
                    case "importAccessories":
                        return;
                    case "delete":
                        return this.handleDelete();
                    case "upload":
                        return this.handleFile();
                    case "detail":
                        return this.handleDetail();
                    default:
                        return;
                }
            },
            uploadError(err, file, fileList) {
                console.log(err, file, fileList);
                this.$message.error("附件上传失败!");
            },
            notify(res, file) {
                console.log(res, file);
                this.$message.success("附件上传成功!");
            },
            handleStatus() {
                let ops = this.row.status.value == "EFFECTIVED" ? "禁用" : "激活";
                let status =
                    this.row.status.value == "EFFECTIVED" ? "INVALID" : "EFFECTIVED";
                this.$confirm("此操作将" + ops + "该用户, 是否继续?", ops + "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.$api.user.status(this.row.id, status, () => {
                        this.$message({
                            type: "success",
                            message: ops + "用户成功",
                            onClose: this.$emit("deleteDone", this.row)
                        });
                    });
                });
            },
            handleDelete() {
                this.$confirm("此操作将删除该用户, 是否继续?", "删除提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.$api.farmer.delete(this.row.id, () => {
                        this.$message({
                            type: "success",
                            message: "删除用户成功",
                            onClose: () => {
                                this.$emit("deleteDone", this.row);
                            }
                        });
                    });
                });
            },
            handleReset() {
                this.$confirm("此操作将重置该用户密码, 是否继续?", "重置密码提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.$api.user.reset(this.row.id, () => {
                        this.$message({
                            type: "success",
                            message: "重置密码成功",
                            onClose: () => {
                                this.$emit("deleteDone", this.row);
                            }
                        });
                    });
                });
            },
            handleEdit() {
                this.$emit("editRow", this.row);
            },
            handleGrant() {
                this.$emit("grantShow", this.row);
            },
            handleFile() {
                this.$emit("fileRow", this.row);
            },
            handleDetail(){
                this.$router.push({path:"/farmer/detail",query:{userId:this.row.id}})
            }
        }
    };
</script>
